from src.core.interface.iTask import ITask
from src.core.util.util import to_camelcase, find_java_type, write_sql_search_fields, find_mybatis_jdbc_type


class SqlMapTask(ITask):
    @property
    def filename(self) -> str:
        return 'sql_map.txt'

    def build_column_str(self, column_data, index, total_count) -> str:
        return '<result property="{}" column="{}" />\n'.format(to_camelcase(column_data[0]), column_data[0])


class AllFieldsTask(ITask):
    @property
    def filename(self) -> str:
        return 'all_fields.txt'

    def build_column_str(self, column_data, index, total_count) -> str:
        line: str = '`{}` AS {}'.format(column_data[0], to_camelcase(column_data[0]))
        if index < total_count - 1:
            return line + ',\n'
        return line


class AllFieldsNoAsTask(ITask):
    @property
    def filename(self) -> str:
        return 'all_fields_no_as.txt'

    def build_column_str(self, column_data, index, total_count) -> str:
        line: str = '`{}`'.format(column_data[0])
        if index < total_count - 1:
            return line + ',\n'
        return line


class AllValuesTask(ITask):

    @property
    def filename(self) -> str:
        return 'all_values.txt'

    def build_column_str(self, column_data: [str], index: int, total_count: int) -> str:
        res = '#{{item.{}, jdbcType={}}}'.format(to_camelcase(column_data[0]), find_mybatis_jdbc_type(column_data[1]))
        if index < total_count - 1:
            return res + ',\n'
        return res


class AllValuesNoItemTask(ITask):

    @property
    def filename(self) -> str:
        return 'all_values_no_item.txt'

    def build_column_str(self, column_data: [str], index: int, total_count: int) -> str:
        res = '#{{{}, jdbcType={}}}'.format(to_camelcase(column_data[0]), find_mybatis_jdbc_type(column_data[1]))
        if index < total_count - 1:
            return res + ',\n'
        return res


class JavaAttributeTask(ITask):
    @property
    def filename(self) -> str:
        return 'java_attributes.txt'

    def build_column_str(self, column_data, index, total_count) -> str:
        return ('/**\n' +
                ' * {}\n'.format(column_data[2]) +
                ' */\n' +
                'private {} {};\n\n'.format(find_java_type(column_data[1]), to_camelcase(column_data[0])))


class SearchFieldsSingleParamTask(ITask):

    @property
    def filename(self) -> str:
        return 'search_fields_single_param.txt'

    def build_column_str(self, column_data: [str], index: int, total_count: int) -> str:
        return write_sql_search_fields(column_data, is_multi=False)


class SearchFieldsMultiParamsTask(ITask):

    @property
    def filename(self) -> str:
        return 'search_fields_multi_params.txt'

    def build_column_str(self, column_data: [str], index: int, total_count: int) -> str:
        return write_sql_search_fields(column_data, is_multi=True, param_idx=0)


class SetValuesTask(ITask):

    @property
    def filename(self) -> str:
        return 'set_values.txt'

    def build_column_str(self, column_data: [str], index: int, total_count: int) -> str:
        camel_name = to_camelcase(column_data[0])
        if index < total_count - 1:
            return ('<if test="{} != null" >\n'.format(camel_name) +
                    '    `{}` = #{{{},jdbcType={}}},\n'
                    .format(column_data[0], camel_name, find_mybatis_jdbc_type(column_data[1])) +
                    '</if>\n')
        return ('<if test="{} != null" >\n'.format(camel_name) +
                '    `{}` = #{{{},jdbcType={}}}\n'
                .format(column_data[0], camel_name, find_mybatis_jdbc_type(column_data[1])) +
                '</if>\n')


class JsonTask(ITask):
    @property
    def filename(self) -> str:
        return 'json.txt'

    def build_column_str(self, column_data: [str], index: int, total_count: int) -> str:
        res = '"{}": null'.format(to_camelcase(column_data[0]))
        if index < total_count - 1:
            return res + ',\n'
        return res
